package cn.cloud.project.loadBalance.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author: cql
 * @date: 2024/11/4
 * @description:
 */
@RestController
@RequestMapping("/loadBalance")
public class LoadBalanceController {

    @Resource
    private RestTemplate restTemplate;

    /**
     * 测试默认的负载均衡策略
     */
    @GetMapping("/default")
    public String testDefaultLoadBalanced() {
        ResponseEntity<String> response = restTemplate.getForEntity("http://demo-provider/provider/test/loadBalanced", String.class);
        return "测试注册中心 与 负载均衡组件的整合能力: " + response.getBody();
    }

    /**
     * 测试切换为随机负载均衡器
     */
    @GetMapping("/random")
    public String testRandomLoadBalanced() {
        ResponseEntity<String> response = restTemplate.getForEntity("http://demo-consumer/consumer/test", String.class);
        return "测试注册中心 与 负载均衡组件的整合能力: " + response.getBody();
    }

    /**
     * 测试切换为随机负载均衡器
     */
    @GetMapping("/timeout")
    public String testTimeOut() {
        ResponseEntity<String> response = restTemplate.getForEntity("http://demo-consumer/timeout", String.class);
        return "测试LoadBalance超时重试机制: " + response.getBody();
    }


}
